var app = new Vue({
    el: '#app',
    data: {
        community: {
            id: 0
        },
        buildingList: {},
        buildingDetail: {},
        dialog: {
            isShow: false,
            disabled: false
        },
        communityStyle: ''
    },
    mounted:function(){
        this.community.id = this.getUrlKey('id');
        this.getCommunityDetail()
    },
    methods: {
        /**
         * 获取小区详情
         */
        getCommunityDetail: function() {
            var _this = this;
            $.ajax({
                url: '/community/getOne',
                type: 'get',
                data: _this.community,
                dataType: 'json',
                success: function(res) {
                    if (res.status === 0) {
                       _this.community = res.data.community;
                       _this.buildingList = res.data.building;
                       if (_this.community.thumb.length > 0) {
                           _this.communityStyle = 'background: url("'+_this.community.thumb[0].url+'") no-repeat center;background-size: 100% 100%;'
                       }
                    } else {
                        _this.$message.error(res.message)
                    }
                }
            })
        },
        /**
         * 显示楼栋信息
         * @param row
         */
        showBuildingDetail: function(row) {
            var _this = this;
            if (row.id) {
                _this.getBuildingDetail(row.id, function(res) {
                    _this.buildingDetail = res.data.building;
                    _this.dialog.isShow = true;
                })
            } else {
                _this.buildingDetail = row;
                _this.dialog.isShow = true;
            }
        },
        /**
         * 获取楼栋信息
         * @param success
         * @param id
         */
        getBuildingDetail: function(id, success) {
            var _this = this;
            $.ajax({
                url: '/building/getOne',
                type: 'get',
                data: {id: id},
                dataType: 'json',
                success: function(res) {
                    if (res.status === 0) {
                        success(res)
                    } else {
                        _this.$message.error(res.message)
                    }
                }
            })
        },
        /**
         * 楼栋保存
         * @returns {boolean}
         */
        buildingSave: function() {
            var _this = this;
            if (!_this.buildingDetail.build_num) {
                _this.$message.error('请输入楼栋编号');
                return false;
            }
            if (!_this.buildingDetail.name) {
                _this.$message.error('请输入楼栋名称');
                return false;
            }
            _this.dialog.disabled = true;
            _this.buildingDetail.community_id = _this.community.id;
            $.ajax({
                url: '/building/save',
                type: 'post',
                data: _this.buildingDetail,
                dataType: 'json',
                success: function(res) {
                    if (res.status === 0) {
                        _this.buildingDetail.id = res.data.id;
                        _this.$message.success(res.message);
                    } else {
                        _this.$message.error(res.message)
                    }
                    _this.dialog.disabled = false;
                }
            })
        },
        /**
         * 前往楼栋详情
         * @param row
         */
        goBuildingDetail: function(row) {
            window.location.href = '/building/detail?id=' + row.id;
        },
        /**
         * 前往图纸详情
         * @param row
         */
        goToDrawing: function(row) {
            window.location.href = '/community/drawing?id=' + this.community.id;
        },
        /**
         * 上传成功
         */
        fileSuccess: function(response, file, fileList, field) {
            var _this = this;
            if (response.status === 0) {
                file.id = response.data.id;
                file.raw = {};
                _this.buildingDetail[field] = fileList;
                _this.$message.success(response.message)
            } else {
                _this.$message.error(response.message)
            }
        },
        /**
         * 图片预览
         */
        filePreview: function(file) {
            console.log(file)
        },
        /**
         * 图片删除
         */
        fileRemove: function(file, fileList, field) {
            var _this = this;
            _this.buildingDetail[field] = fileList;
            _this.$message.success('删除成功！')
        },
        fileExceed: function() {
            this.$message.warning('只允许上传一个文件！');
        }
    }
});
